﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BenevolentSun
{
    public class MainMenuScreen : Screen
    {

        Sprite background;

        Sprite logo;

        InstructionsControl instructionsControl;

        HorizontalMenuControl horizontalMenuControl;

        DimmerControl dimmerControl;


        public override void LoadContent()
        {
            /*
            SpriteFont font = Content.Load<SpriteFont>("Fonts/Ebrima30");
            menu = new TextMenu();
            menu.SelectedColor = new Color(204, 204, 204, (byte)(1 * 255));
            menu.UnselectedColor = new Color(51, 51, 51, (byte)(1 * 255));
            if (Session.IsGameComplete == false)
            {
                if (Session.Level == 1)
                    menu.AddEntry(new TextField("Play", font));
                else
                    menu.AddEntry(new TextField("Continue", font));
            }
            menu.AddEntry(new TextField("Free Stage", font));
            menu.AddEntry(new TextField("Options", font));
            menu.AddEntry(new TextField("Exit", font));
            menu.SetPositions(new Vector2(0, 150), new Vector2(0, 30));
            */

            logo = new Sprite(Content.Load<Texture2D>("Textures/Gui/Logo"));

            background = new Sprite(Content.Load<Texture2D>("Textures/Gui/Background"));

            instructionsControl = new InstructionsControl(Content);

            SpriteFont horizontalMenuFont = Content.Load<SpriteFont>("Fonts/Horizontal Menu Font");
            horizontalMenuControl = new HorizontalMenuControl(Content);
            if (Session.IsGameComplete == false)
            {
                if (Session.Level == 1)
                    horizontalMenuControl.AddEntry(new TextField("Play", horizontalMenuFont, new Color(255, 0, 0)));
                else
                    horizontalMenuControl.AddEntry(new TextField("Continue", horizontalMenuFont, new Color(255, 0, 0)));
            }
            else
            {
                horizontalMenuControl.AddEntry(new TextField("Play", horizontalMenuFont, new Color(255, 0, 0)));
            }
            horizontalMenuControl.AddEntry(new TextField("Select Stage", horizontalMenuFont, new Color(0, 102, 255)));
            horizontalMenuControl.AddEntry(new TextField("Options", horizontalMenuFont, new Color(0, 255, 0)));
            horizontalMenuControl.AddEntry(new TextField("Exit", horizontalMenuFont, new Color(255, 255, 0)));
            horizontalMenuControl.Initialize();

            dimmerControl = new DimmerControl(Content);

            doInit();
            //doAnimateIn();

            base.LoadContent();
        }


        #region States
        State state;

        enum State
        {
            Start,
            AnimateIn,
            AnimateOut
        }

        private void doInit()   // kind of like start
        {
            state = State.Start;
            instructionsControl.SetInstructions("Press Start");
            instructionsControl.FadeInInstructions();
        }

        private void doStart()
        {
            state = State.Start;
            instructionsControl.SetInstructions("Press Start");
            instructionsControl.FadeInInstructions();

            dimmerControl.FadeOut();
            horizontalMenuControl.FadeOut();
        }

        private void doAnimateIn()
        {
            state = State.AnimateIn;
            instructionsControl.FadeOutInstructions();
            dimmerControl.FadeIn();
            horizontalMenuControl.FadeIn();
            refreshMenuInstructions();
        }
        #endregion


        private void refreshMenuInstructions()
        {
            if (horizontalMenuControl.Selected.Text == "Play")          // start a new game
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Start playing Switch.");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Continue")          // start a new game
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Continue from the last stage cleared.");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Select Stage")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("View records or play any unlocked stage.");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Options")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Switch the game options.");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Exit")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Leaving so soon?");
                instructionsControl.FadeInInstructions();
            }
        }


        public override void HandleInput()
        {
            if (state == State.Start)
            {
                if (ActionMap.Start)
                {
                    doAnimateIn();
                }
            }
            else if (state == State.AnimateIn)
            {
                if (ActionMap.Right)
                {
                    if (horizontalMenuControl.Next())
                    {
                        refreshMenuInstructions();
                        AudioManager.PlayCue("Pop");
                    }
                }

                if (ActionMap.Left)
                {
                    if (horizontalMenuControl.Prev())
                    {
                        refreshMenuInstructions();
                        AudioManager.PlayCue("Pop");
                    }
                }

                if (ActionMap.Confirm)
                {
                    if (horizontalMenuControl.Selected.Text == "Play")          // start a new game
                    {
                        // open the level here
                        ScreenManager.AddScreen(new LevelScreen(0));
                        ExitScreen();
                    }
                    else if (horizontalMenuControl.Selected.Text == "Continue")          // start a new game
                    {
                        // open the level here
                        ScreenManager.AddScreen(new LevelScreen(Session.Level));
                        ExitScreen();
                    }
                    else if (horizontalMenuControl.Selected.Text == "Select Stage")
                    {
                        ScreenManager.AddScreen(new LevelSelectionScreen());
                        ExitScreen();
                    }
                    else if (horizontalMenuControl.Selected.Text == "Options")
                    {
                        OptionScreen optionScreen = new OptionScreen();
                        optionScreen.OnRemove += new EventHandler(optionScreen_OnRemove);
                        ScreenManager.AddScreen(optionScreen);
                        horizontalMenuControl.FadeOut();
                        horizontalMenuControl.AdvanceToEnd();
                    }
                    else if (horizontalMenuControl.Selected.Text == "Exit")
                    {
                        ScreenManager.Game.Exit();
                    }
                }

                if (ActionMap.Cancel)
                {
                    doStart();
                }


                if (InputManager.IsNewKeyPress(Keys.T))
                {
                    ScreenManager.AddScreen(new GameOverScreen());
                    ExitScreen();
                }
            }

            base.HandleInput();
        }

        void optionScreen_OnRemove(object sender, EventArgs e)
        {
            ((OptionScreen)sender).OnRemove -= optionScreen_OnRemove;
            horizontalMenuControl.FadeIn();
        }


        public override void Update(GameTime gameTime, bool isFocusAvailable, bool isOccluded)
        {
            /*
            menu.Update(gameTime);
            */

            instructionsControl.Update(gameTime);
            horizontalMenuControl.Update(gameTime);
            dimmerControl.Update(gameTime);

            base.Update(gameTime, isFocusAvailable, isOccluded);
        }


        public override void Draw(GameTime gameTime)
        {
            /*
            SpriteBatch.Begin();
            menu.Draw(SpriteBatch);
            SpriteBatch.End();
            */

            SpriteBatch.Begin();

            background.Draw(SpriteBatch);

            logo.Draw(SpriteBatch);

            dimmerControl.Draw(SpriteBatch);

            horizontalMenuControl.Draw(SpriteBatch);

            instructionsControl.Draw(SpriteBatch);

            SpriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
